'use strict'
const path = require('path')

function resolve(dir) {
    return path.join(__dirname, dir)
}

const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')

const isProduction = process.env.NODE_ENV == 'production'
const isDev = !isProduction

const name = process.env.VUE_APP_TITLE
const port = process.env.PORT
console.log('ENV', process.env.NODE_ENV)

module.exports = {
    publicPath: './',
    outputDir: process.env.OUT_PUT_DIR,
    lintOnSave: isDev,
    productionSourceMap: false,
    devServer: {
        host: 'localhost',
        port: port,
        open: false,
        proxy: {
            [process.env.VUE_APP_BASE_API]: {
                target: process.env.API_HOST,
                changeOrigin: true,
                pathRewrite: {
                    ['^' + process.env.VUE_APP_BASE_API]: ''
                }
            }
        },
        client: {
            overlay: false
        }
    },

    configureWebpack: (config) => {
        config.name = name
        config.devtool = isDev ? 'eval-source-map' : false
        config.resolve.alias = {
            '@': resolve('src')
        }
        config.module.noParse = /^(vu|vue-router|vuex|vuex-router-sync|axios|element-ui)$/

        if (isProduction) {
            config.output.filename = 'js/[name].[contenthash:8].min.js'
            config.output.chunkFilename = 'js/[name].[contenthash:8].min.js'
        } else {
            config.output.filename = 'js/[name].js'
            config.output.chunkFilename = 'js/[name].js'
        }

        config.cache = {
            type: 'filesystem',
            allowCollectingMemory: true
        }
    },

    chainWebpack(config) {
        config.plugin('speed-measure-webpack-plugin').use(SpeedMeasurePlugin).end()
        config.plugins.delete('preload')
        config.plugins.delete('prefetch')
        config.plugin('define').tap((definitions) => {
            Object.assign(definitions[0]['process.env'], {
                MOCK: process.env.MOCK
            })
            return definitions
        })

        config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end()
        config.module
            .rule('icons')
            .test(/\.svg$/)
            .include.add(resolve('src/assets/icons'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
            .end()
        config.optimization.splitChunks({
            chunks: 'all',
            cacheGroups: {
                elementUI: {
                    name: 'chunk-elementUI',
                    priority: 20,
                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/
                },
                commons: {
                    name: 'chunk-commons',
                    test: resolve('src/components'),
                    minChunks: 3,
                    priority: 5,
                    reuseExistingChunk: true
                }
            }
        })
        config.optimization.runtimeChunk('single'),
            {
                from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
                to: './' //到根目录下
            }
    }
}
